package com.ultrasoft.farmasoft.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ultrasoft.farmasoft.domain.Pharmacy;
import com.ultrasoft.farmasoft.domain.User;
import com.ultrasoft.farmasoft.domain.Userpharmacy;
import com.ultrasoft.farmasoft.service.HumanResourcesService;

@RestController
@SessionAttributes({"s_UserPharmacy"}) 
@RequestMapping("/service")
public class WebServiceLoginListController {
	
	private final HumanResourcesService humanResourceService;

	@Autowired
    public WebServiceLoginListController(HumanResourcesService humanResourceService) {
        this.humanResourceService = humanResourceService;
    }
	@RequestMapping(value="/indexServiceLoginList",method = RequestMethod.POST)
	protected List<Userpharmacy> onSubmitIndexService(@RequestParam String login,@RequestParam String password,HttpServletRequest request){    	
		return getListUserPharmacyTemp(humanResourceService.getListUserPharmacy(login, password));
    }
	private List<Userpharmacy> getListUserPharmacyTemp(List<Userpharmacy> list){
		List<Userpharmacy> tempList = new ArrayList<Userpharmacy>();
		for (Userpharmacy userpharmacy : list) {
			Userpharmacy userPharmacyTemp = new Userpharmacy(userpharmacy.getUserpharmacyPK().getPharmacyId(),userpharmacy.getUserpharmacyPK().getUserId(),userpharmacy.getActive());
			User userTemp = new User(userpharmacy.getUser().getUserId(),userpharmacy.getUser().getName(),userpharmacy.getUser().getLastName(),userpharmacy.getUser().getCi(),userpharmacy.getUser().getGenero(),userpharmacy.getUser().getPhone(),userpharmacy.getUser().getEmail(),userpharmacy.getUser().getLogin(),userpharmacy.getUser().getPassword());
			userPharmacyTemp.setUser(userTemp);
			Pharmacy pharmacyTemp = new Pharmacy(userpharmacy.getPharmacy().getPharmacyId(),userpharmacy.getPharmacy().getName(),userpharmacy.getPharmacy().getClient());
			userPharmacyTemp.setPharmacy(pharmacyTemp);
			tempList.add(userPharmacyTemp);
		}
		return tempList;
	}
}
